package com.kylin.module.admin.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.criterion.Order;

import com.kylin.common.Constants;
import com.kylin.common.entity.MsgKey;
import com.kylin.common.paginate.DisplayPage;
import com.kylin.common.paginate.PageManager;
import com.kylin.hibernate.CriteriaInfo;
import com.kylin.hibernate.PropertyCondition;
import com.kylin.module.admin.business.ManageMemberBlogic;
import com.kylin.module.admin.business.ManagePostBlogic;
import com.kylin.module.admin.business.ManageShopBlogic;
import com.kylin.module.community.orm.Member;
import com.kylin.module.community.orm.Post;
import com.kylin.struts.BaseDispatchAction;

/**
 * 后台管理接口：管理社区帖子
 * @author lzh
 *
 */
public class ManagePostAction  extends BaseDispatchAction {

	protected static Logger log = Logger.getLogger(ManagePostAction.class);
	
	/**
	 * 查询已审核通过的贴子列表
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward searchPost(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.debug("查询贴子列表 " );
		String forward = "listPost";
		int pageSize = 20;// 分页列表的每页行数  
		int pageNum = 1;  // 当前页号
		String searchId = request.getParameter("searchId");
		CriteriaInfo cinfo = null;
		try {
			if (request.getParameter("page") != null) {
				pageNum = Integer.parseInt(request.getParameter("page"));
			}
			if (searchId == null) {
				List condsList = new LinkedList();
//				condsList.add(new PropertyCondition("user.userrole",Constants.USER_ROLE_MEMBER,PropertyCondition.OPERATOR_EQ));
				if( hasParam(request.getParameter("active")) ){
					String active = request.getParameter("active");
					condsList.add(new PropertyCondition("active",new Short(active),PropertyCondition.OPERATOR_EQ));
				}
				if( hasParam(request.getParameter("title")) ){
					String title = request.getParameter("title");
					condsList.add(new PropertyCondition("title","%"+title+"%",PropertyCondition.OPERATOR_LIKE));
				}
				if( hasParam(request.getParameter("content")) ){
					String content = request.getParameter("content");
					condsList.add(new PropertyCondition("content","%"+content+"%",PropertyCondition.OPERATOR_LIKE));
				}
				if( hasParam(request.getParameter("userName")) ){
					String userName = request.getParameter("userName");
					condsList.add(new PropertyCondition("user.username","%"+userName+"%",PropertyCondition.OPERATOR_LIKE));
				}
				if( hasParam(request.getParameter("name")) ){
					String name = request.getParameter("name");
					condsList.add(new PropertyCondition("user.name","%"+name+"%",PropertyCondition.OPERATOR_LIKE));
				}
				if( hasParam(request.getParameter("language")) ){
					String language = request.getParameter("language");
					condsList.add(new PropertyCondition("language",language,PropertyCondition.OPERATOR_EQ));
				}
				if( hasParam(request.getParameter("startDate")) ){
					Date date =  new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("startDate"));
					condsList.add(new PropertyCondition("createdon",date,PropertyCondition.OPERATOR_GE));
				}
				if( hasParam(request.getParameter("endDate")) ){
					Date date =  new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("endDate"));
					condsList.add(new PropertyCondition("createdon",date,PropertyCondition.OPERATOR_LE));
				}
				cinfo = new CriteriaInfo(Post.class, condsList);
				cinfo.setOrderby(Order.desc("createdon"));
				addUserCacheCriteriaInfo(request, cinfo);
			}else{
				cinfo = getUserCacheCriteriaInfo(request, searchId);
			}
			DisplayPage page = PageManager.getDisplayPage(pageSize, pageNum, cinfo);
			request.setAttribute(Constants.KEY_REQUEST_RESULT, page);
		}catch (Exception e) {
			doException( request, e, forward);
		}
		return mapping.findForward(forward);
	}


	/**
	 * 根据指定的帖子id 查看帖子
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward detail(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
		log.debug("查看贴子: "+request.getParameter("id"));
		String forward = "detailPost";
		String id = request.getParameter("id");
		try{
			ManagePostBlogic bean = new ManagePostBlogic();
			Post post = bean.selectPostById(new Long(id));
			request.setAttribute(Constants.KEY_REQUEST_RESULT, post);
		}catch (Exception e) {
			doException( request, e, forward);;
		}
		return mapping.findForward(forward);
	}

	/**
	 * 根据指定的帖子id 删除帖子
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
		log.debug("删除贴子: "+request.getParameter("id"));
		String forward = "listPost";
		String id = request.getParameter("id");
		try{
			ManagePostBlogic bean = new ManagePostBlogic();
			bean.deletePostById(new Long(id));
			return searchPost( mapping, actionForm, request, response);
		}catch (Exception e) {
			forward = doException( request, e,forward);
			return mapping.findForward(forward);
		}
	}
	/**
	 * 审核发布帖子
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward activationCompany(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		String forward = "detailPost";
		try {
			Long id = new Long(request.getParameter("id"));
			ManagePostBlogic bean = new ManagePostBlogic();
			bean.allowPostById(id);
			request.setAttribute(Constants.MSGKEY, new MsgKey("","发布帖子成功"));
			return searchPost( mapping, actionForm, request, response);
		}catch (Exception e) {
			doException(request, e,forward);
			request.setAttribute(Constants.KEY_REQUEST_RESULT, "发布帖子失败");
		}
		return mapping.findForward(forward);
	}

}
